import {
  post
} from '../../utils/request';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    config: null,

    detail: null,

    globalData: app.globalData,

    fixedHeight: app.globalData.titleHeight + app.globalData.statusBarHeight + 45,
    fixedhd: false,

    currentTab: -1,
    currentFloor: null,
    heightList: [],

    slide: {
      vertical: false,
      autoplay: true,
      interval: 2000,
      duration: 500,
    },

    showQuanyiModal: false,

  },

  // 立即抢购
  onBuy() {
    app.checkLogin(true, () => {
      wx.showLoading({
        title: '正在提交',
        mask: true
      });
      post("/order/pre-add", {
        type: 2,
        id: this.data.detail.id,
        subId: 0,
        num: 1
      }).then(res => {
        wx.hideLoading();
        if (res.code === 0) {
          wx.navigateTo({
            url: `/pages/order/preview?id=${res.data.id}`,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'error'
          });
          return;
        }
      });
    });
  },

  // 商品详情
  toGoods(e) {
    let {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/goods/detail?id=${id}`,
    });
  },

  // 服务详情
  toServe(e) {
    let {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/serve/detail?id=${id}`,
    });
  },

  // 获取数据
  getDetail(id) {
    post(`/mall/pkg/detail/${app.globalData.tenantCode}/${id}`)
      .then(resp => {
        if (resp.code === 0) {
          this.setData({
            detail: resp.data
          });
        } else {
          wx.showModal({
            title: '提示',
            content: resp.msg,
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.navigateBack();
              }
            }
          })
        }
      });
  },

  // 套餐权益模态框关闭
  closeQuanyiModal() {
    this.setData({
      showQuanyiModal: false
    });
  },

  // 套餐权益模态框打开
  openQuanyiModal() {
    this.setData({
      showQuanyiModal: true
    });
  },

  // 返回
  goBack() {
    wx.navigateBack();
  },

  // 首页
  onHome() {
    wx.reLaunch({
      url: '/pages/home/index',
    });
  },

  // 点击标签
  onChgTab(e) {
    this.setData({
      currentFloor: `floor${e.currentTarget.dataset.tab}`,
    });
  },

  // 滚动监听
  changeScroll(e) {
    // 获取距离顶部的距离
    let scrollTop = e.detail.scrollTop;

    if (scrollTop >= this.data.globalData.windowWidth - this.data.fixedHeight) {
      if (!this.data.fixedhd) {
        this.setData({
          fixedhd: true,
        });
      }
    } else {
      if (this.data.fixedhd) {
        this.setData({
          fixedhd: false,
          currentFloor: null,
        });
      }
    }
  },

  // 分享
  onShare() {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let options = app.getPageOptions();
    this.getDetail(options.id);

    app.getConfig(config => {
      this.setData({
        config
      });
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: this.data.detail.name,
      path: `/pages/pkg/detail?id=${this.data.detail.id}`,
      imageUrl: this.data.detail.img
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareTimeline() {
    return {
      title: this.data.detail.name,
      path: `/pages/pkg/detail?id=${this.data.detail.id}`,
      imageUrl: this.data.detail.img
    }
  },


});